#include <iostream>
#include <cstdio>
#include <vector>
#include <algorithm>
#include <cstring>

const int N = 8e3 + 5;
const int M = 3e4 + 5;

int n, m;
int x[N], y[N], a[N];
std::vector<std::pair<int, int>> adj[M << 1];
bool vis[M << 1];
unsigned long long bitset[N][N / 64 + 5];
int sum[N / 16 + 5][1 << 16], pc[1 << 16];

inline int lowbit(int x) {
	return x & (-x);
}

void solve() {
	for(int s = 1; s < (1 << 16); ++s) pc[s] = pc[s >> 1] + (s & 1);
	for(int i = 0; 16 * i <= n; ++i) {
		//i * 16, i * 16 + 15
		sum[i][0] = 0;
		for(int j = 0; j < 16; ++j) sum[i][1 << j] = a[i * 16 + j];
		for(int s = 1; s < (1 << 16); ++s) {
			if(pc[s] == 1) continue;
			int t = s & (s - 1);
			sum[i][s] = sum[i][t] + sum[i][s ^ t];
		}
	}
	std::memset(bitset, 0, sizeof bitset);
	for(int i = 1; i <= 2 * m; ++i) adj[i].clear();
	for(int i = 1; i <= n; ++i) for(int j = i + 1; j <= n; ++j) {
		adj[abs(x[i] - x[j]) + abs(y[i] - y[j])].emplace_back(i, j);
	}
	long long ans = 0;
	int c = 0;
	int b = (1 << 16) - 1;
	for(int k = 1; k <= 2 * m; ++k) {
		for(auto e : adj[k]) {
			int u = e.first, v = e.second;
			int cnt = 0;
			if(!vis[k] && (k <= 10 || k % 20 == 3)) {
				for(int i = 0; i * 64 <= n; ++i) {
					unsigned long long mask = bitset[u][i] ^ bitset[v][i];
					cnt += pc[mask >> 0 & b];
					cnt += pc[mask >> 16 & b];
					cnt += pc[mask >> 32 & b];
					cnt += pc[mask >> 48 & b];
					ans += sum[i << 2 | 0][mask >> 0 & b];
					ans += sum[i << 2 | 1][mask >> 16 & b];
					ans += sum[i << 2 | 2][mask >> 32 & b];
					ans += sum[i << 2 | 3][mask >> 48 & b];
				}
				ans += 1ll * cnt * (a[u] + a[v]);
			}
			bitset[u][v / 64] |= 1ull << (v % 64);
			bitset[v][u / 64] |= 1ull << (u % 64);
		}
	}
	std::cout << ans << "\n";
}

int main() {
	std::ios::sync_with_stdio(false);
	std::cin.tie(nullptr), std::cout.tie(nullptr);

	std::freopen("chess.in", "r", stdin);
	std::freopen("chess.out", "w", stdout);

	int t; std::cin >> t;
	while(t--) {
		std::cin >> n >> m;
		std::memset(a, 0, sizeof a);
		vis[1] = 1;
		for(int i = 2; i <= 2 * m; ++i) {
			if(!vis[i]) {
				for(int j = 2 * i; j <= 2 * m; j += i) vis[j] = 1;
			}
		}
		for(int i = 1; i <= n; ++i) std::cin >> x[i] >> y[i];
		for(int i = 1; i <= n; ++i) std::cin >> a[i];
		solve();
	}

	return 0;
}
